package com.zhao.common.aop;

import com.zhao.common.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class ControllerLogAspect {

    @Pointcut("within(com.zhao.controller..*)")
    public void inWebLayer() {
    }

    @Around(value = "inWebLayer()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Class targetClass = pjp.getTarget().getClass();
        String methodName = pjp.getSignature().getName();
        Class[] parameterTypes = ((MethodSignature) pjp.getSignature()).getMethod().getParameterTypes();
        Method method = targetClass.getMethod(methodName, parameterTypes);
        Object[] arguments = pjp.getArgs();
        Object obj = pjp.proceed();
        log.info("class:{},method:{},arg:{},resp:{},cost:{}", targetClass, methodName, arguments, JacksonUtil.serialize(obj), System.currentTimeMillis() - start);
        return obj;
    }
}
